<template>
	<view class="container">
		<form :report-submit="true" @submit="formLogin">
			<view class="input_content">

				<view class="input_item">
					<tui-icon name="mobile" :size="20" color='#999999'></tui-icon>
					<input placeholder-class="phcolor" class="is_input" name="mobile" placeholder="请输入手机号码" maxlength="11" v-model="mobile"
					 type="number" />
				</view>

				<view class="input_item">
					<tui-icon name="shield" :size="20" color='#999999'></tui-icon>
					<input placeholder-class="phcolor" class="is_input" name="smsCode" placeholder="请输入验证码" maxlength="6" type="number" />
					<view class="codeimg" :type="type" :plain="true" :disabled="disabled" @click="btnSend">{{btnText}}</view>
				</view>
				
				<view class="input_item">
					<tui-icon name="pwd" :size="20" color='#999999'></tui-icon>
					<input placeholder-class="phcolor" class="is_input" name="password" placeholder="请输入新密码" maxlength="11" v-model="password" type="number" />
				</view>
				
				<view class="input_item">
					<tui-icon name="pwd" :size="20" color='#999999'></tui-icon>
					<input placeholder-class="phcolor" class="is_input" name="ConfirmPass" placeholder="再次输入密码" maxlength="11" v-model="ConfirmPass" type="number" />
				</view>

				<view class="loginbtn">
					<button :disabled="butSubmit" :loading="butLoading" hover-class="btn-hover" form-type="submit">确 认</button>
				</view>

			</view>
		</form>
	</view>
</template>

<script>
	import util from "@/utils/util.js"

	export default {
		data() {
			return {
				disabled: false,
				btnText: "获取验证码",
				mobile: "",
				password:"",
				ConfirmPass:"",
				type: "primary",
				codeSubmit:false,
				butSubmit: false,
				butLoading: false,
				state:"",
			}
		},
		onLoad(options){
			this.state = options.state;
			
			let title = '设置密码';
			if (this.state == '1') {
				title = '修改密码'				
			}
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			// 短信验证接口
			getRandom: function() {
				let _this = this;
				_this.$http.post(_this.tui.interfaceUrl() + '/app/user/sendAuthCode', {
				    mobile: this.mobile
				    
				}).then(res => {
					_this.codeSubmit = true;
					if(res.data.errno == 500){
						_this.tui.toast(res.data.errmsg);
					}else{
						console.log(res.data.errmsg)
					}
					
				}).catch(err => {
					console.log(err)
				})				
			},
			doLoop: function(seconds) {
				this.getRandom();
				seconds = seconds ? seconds : 60;
				this.btnText = seconds + "s后获取";
				let countdown = setInterval(() => {
					if (seconds > 0) {
						this.btnText = seconds + "s后获取"
							--seconds;
					} else {
						this.btnText = "获取验证码";
						this.disabled = false;
						this.type = "primary";
						this.codeSubmit = false;
						clearInterval(countdown);
					}
				}, 1000);
			},

			btnSend: function() {
				if (util.isNullOrEmpty(this.mobile)) {
					this.tui.toast('请输入手机号码');
					return
				} else if (!util.isMobile(this.mobile)) {
					this.tui.toast('手机号输入有误，请重新输入');
					return
				}
				this.disabled = true;
				this.type = "gray"
				if(!this.codeSubmit){
					this.btnText = "请稍候...";
					setTimeout(() => {
						this.doLoop(60)
					}, 500)
				}else{
					console.log(this.codeSubmit)
				}
			},

			formLogin(e) {
				let _this = this;
				let loginCode = e.detail.value.smsCode;
				let mobile = e.detail.value.mobile;
				let password = e.detail.value.password;
				let ConfirmPass = e.detail.value.ConfirmPass;
				if (util.isNullOrEmpty(mobile)) {
					_this.tui.toast('请输入手机号码');
					return
				} else if (!util.isMobile(mobile)) {
					util.toast('手机号输入有误，请重新输入');
					return
				} else if (util.isNullOrEmpty(loginCode)) {
					_this.tui.toast('请输入验证码');
					return
				}
				if (util.isNullOrEmpty(password)) {
					this.tui.toast('请输入密码');
					return
				}
				if (util.isNullOrEmpty(ConfirmPass)) {
					this.tui.toast('请再次输入密码');
					return
				}
				if(password!==ConfirmPass){
				    this.tui.toast('两次密码不一致,请重新输入');
				    return
				}

				_this.butSubmit = true;
				_this.butLoading = true;

				_this.$http.post(_this.tui.interfaceUrl() + '/app/user/findPassword', {
					mobile: mobile,
					authCode: loginCode,
					password: password
				}).then(res => {
					console.log(res);
					if (res.data.errno == 0) {
						setTimeout(() => {
							uni.reLaunch({
								url: '../user/user'
							})
						}, 200);
						_this.butSubmit = false;
						_this.butLoading = false;
					} else {
						_this.$api.msg(res.data.errmsg);
						_this.butSubmit = false;
						_this.butLoading = false;
					}

				}).catch(err => {
					console.log(err)
					_this.butSubmit = false;
					_this.butLoading = false;
				})
			},
		}
	}
</script>

<style>
	/* 表单 */
	/* input框 */
	.input_content {
		width: 85%;
		margin: 0 auto;
		padding-top: 200upx;
	}

	.input_item {
		width: 100%;
		height: 88rpx;
		line-height: 88rpx;
		color: #999999;
		font-size: 14px;
		box-sizing: border-box;
		appearance: none;
		box-shadow: none;
		border-radius: 40px;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 0 30upx;
		border: 2rpx solid #e5e5e5;
		outline: 0;
		font-family: inherit;
		background: #fff;
		resize: none;
		padding-left: 30rpx;
		padding-right: 30rpx;
		margin-bottom: 50upx;
	}

	.input_img {
		width: 20px;
		height: 20px;
	}

	.is_input {
		font-size: $font-base + 2upx;
		color: $font-color-dark;
		width: 100%;
		padding-left: 5px;
	}

	/* 获取验证码 */
	.codeimg {
		position: absolute;
		font-size: 12px;
		right: 9%;
		z-index: 999;
		width: 150rpx;
		text-align: center;
		color: #D14798;
		/* margin-top: -11%; */
		background: #fff;
		border-top-right-radius: 44rpx;
		border-bottom-right-radius: 44rpx;
		height: 80rpx;
		line-height: 80rpx;
		border-left: 1px solid #e5e5e5;
	}

	/* 按钮 */
	.loginbtn {
		width: 100%;
		margin: 10px auto;
	}

	.loginbtn button {
		margin-top: 20rpx;
		height: 88rpx;
		width: 100%;
		line-height: 88rpx;
		color: #ffffff;
		font-size: 32rpx;
		border-radius: 44rpx;
		outline: 0;
		display: block;
		margin: 0;
		font-family: inherit;
		background: #D14798;
		opacity: 0.8;
	}

	button:after {
		border: 2rpx solid #f2f2f2;
	}


	/* 其他登录 */
	.other {
		text-align: center;
		margin-top: 230rpx;
	}

	.othertxt {
		font-size: 14px;
		color: #666666;
		margin-bottom: 60rpx;
	}

	.otherLogin {
		font-size: 14px;
		color: #666666;
		display: flex;
		align-items: center;
		justify-content: space-around;
	}

	.wximg {
		width: 50px;
		height: 47px;
	}

	.dximg {
		width: 50px;
		height: 47px;
	}



	/* 去注册 */
	.toReg {
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 12px;
		height: 24px;
	}

	.toRegRight {
		display: flex;
		align-items: center;
	}

	.toRegtxt {
		padding-left: 13px;
		color: #cccccc;
	}

	.toRegGo {
		color: #ff2431;
	}

	.noPass {
		padding-right: 30rpx;
		color: #0012ff;
	}
</style>
